package com.efounder.util;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.bun.miitmdid.core.JLibrary;
import com.efounder.constant.EnvironmentVariable;

import java.lang.ref.WeakReference;

public class AppContext extends Application {
    private Handler handler;
    private static AppContext instance;
    private static final String TAG = "AppContext";

    public static AppContext getInstance() {
        return instance;
    }


    /**
     * 当前Activity
     **/
    private static WeakReference<Activity> currentActivityWeakReference;

    public static Activity getCurrentActivity() {
        Activity activity = null;
        if (currentActivityWeakReference != null) {
            activity = currentActivityWeakReference.get();
        }
        return activity;
    }

    public static void setCurrentActivity(Activity currentActivity) {
        currentActivityWeakReference = new WeakReference<Activity>(currentActivity);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        JLibrary.InitEntry(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //读取设备唯一id
        loadDeviceOAID();
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                // 表明消息是由该程序发送的
                if (msg.what == 0x111) {
                    // 设置进度条的进度
                    Toast.makeText(instance, "试用版,请申请授权！", Toast.LENGTH_SHORT);
                }
            }
        };

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                setCurrentActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
                if (!activity.equals(getCurrentActivity())) {
                    setCurrentActivity(activity);
                }
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

    /**
     * 加载设备唯一id
     */
    private void loadDeviceOAID() {
        try {
            MiitHelper miitHelper = new MiitHelper(new MiitHelper.AppIdsUpdater() {
                @Override
                public void OnIdsAvalid(boolean isSupport, String oaid, String vaid, String aaid) {
                    if (isSupport && !TextUtils.isEmpty(oaid)) {
                        //支持移动安全联盟设备获取
                        EnvironmentVariable.setProperty("supportMiit", "1");
                        EnvironmentVariable.setProperty("miitOAID", oaid);
                        EnvironmentVariable.setProperty("miitVAID", vaid);
                        EnvironmentVariable.setProperty("miitAAID", aaid);
                    } else {
                        EnvironmentVariable.setProperty("supportMiit", "0");
                    }
                }
            });

            miitHelper.getDeviceIds(this);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "-----------miitID获取失败，抛出异常。。当前版本1.0.10，参照 http://www.msa-alliance.cn/col.jsp?id=120 查看有无新版本");
        }
    }
}
